<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('角色列表')" />
</head>
<body>

<div id="app" v-cloak>
    <div class="main">
        <el-button type="primary" @click="add_role">新增角色</el-button>
        <br><br>
        <el-table :data="tableData" border v-loading="loading">
            <el-table-column fixed label="操作" width="150">
                <template slot-scope="scope">
                    <el-button plain  size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button plain  size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
            <el-table-column fixed label="角色名称" width="250">
                <template slot-scope="scope">
                    <span>{{ scope.row.role_name }}</span>
                </template>
            </el-table-column>

            <el-table-column label="描述" width="500">
                <template slot-scope="scope">
                    <span>{{ scope.row.remark }}</span>
                </template>
            </el-table-column>
            <el-table-column label="当前用户">
                <template slot-scope="scope">
                    <span><el-tag class="m_right m_bottom" v-for="(item,index) in scope.row.admin_list" :key="index">{{ item.username }}</el-tag></span>
                </template>
            </el-table-column>
        </el-table>
    </div>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data(){
            return {
                loading:false,
                tableData:'',
            }
        },
        methods: {
            load_table(){
                this.loading = true;
                let that = this;
                axios.get('/system/get_role_list').then(function (res){
                    that.loading = false;
                    let data = res.data;
                    that.tableData = data;
                }).catch(function(error){
                    that.loading = false;
                    console.log(error);
                });
            },
            add_res(msg,type){
                if(type==null)
                {
                    type='success';
                }
                if(type=='success')
                {
                    this.load_table();
                }
                this.$message({
                    message: msg,
                    type: type
                });
            },
            add_role(){
                this.open_drawer_layer('增加角色','/system/add_role','50%');
            },
            handleEdit(index, row) {
                this.open_drawer_layer('编辑角色','/system/add_role?id='+row.id,'50%');
            },
            handleDelete(index, row) {
                let that = this;
                this.$confirm('是否删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get('/system/delete_role?id='+row.id).then(function (res){
                        let data = res.data;
                        if(data.code==true)
                        {
                            that.$message({
                               message: '删除成功',
                               type: 'success'
                            });
                            that.load_table();
                        }else{
                            that.$message.error(data.msg);
                        }
                    }).catch(function(error){
                        console.log(error);
                    });
                }).catch(() => {

                });
            },
        },
        created() {
            this.load_table();
        },
    })
</script>

</body>
</html>